/**
 * 
 */
package metalink.admin;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;

import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import vo.CollegeResidentialVo;

/**
 * @author Sony
 *
 */
public class ReadHostelListFromExcel extends ActionSupport{
	File excel_file;
	ArrayList<CollegeResidentialVo> hostel_list=new ArrayList<CollegeResidentialVo>();
	



	public ArrayList<CollegeResidentialVo> getHostel_list() {
		return hostel_list;
	}

	public void setHostel_list(ArrayList<CollegeResidentialVo> hostel_list) {
		this.hostel_list = hostel_list;
	}

	public File getExcel_file() {
		return excel_file;
	}

	public void setExcel_file(File excel_file) {
		this.excel_file = excel_file;
	}

	public String execute(){
		try{
			File f=new File("temp.csv");
			FileUtils.copyFile(excel_file, f);
			FileReader fr=new FileReader(f);
			BufferedReader br=new BufferedReader(fr);
			String str="";
			while(null!=(str=br.readLine())){
				System.out.println(str);
				String[] values=str.split(",");
				CollegeResidentialVo vo=new CollegeResidentialVo();
				vo.setResidential_name(values[0]);
				vo.setResidential_type(values[1]);
				vo.setWarden_id(Integer.parseInt(values[2]));
				vo.setWarden_name(values[3]);
				vo.setCapacity(Integer.parseInt(values[4]));
				hostel_list.add(vo);
			}
			HttpSession session = ServletActionContext.getRequest().getSession();
			session.setAttribute("uploaded_hostel_list",hostel_list);
			return SUCCESS;
		}
		catch(Exception e){
			e.printStackTrace();
			return ERROR;
		}
	}
}
